﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using personalplaner.business.dienstplan;
using personalplaner.plugins.plananalysis.viewmodels;
using System.Collections.ObjectModel;
using personalplaner.common.controls;

namespace personalplaner.plugins.plananalysis
{
	/// <summary>
	/// Interaction logic for YearOverview.xaml
	/// </summary>
	public partial class YearOverview : BaseControl
	{
		public YearOverview()
		{
			InitializeComponent();

            var dc = DataContext as YearOverviewViewModel;
            if(dc!= null)
                dc.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(dc_PropertyChanged);
		}

        void dc_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Collection")
                Initialize();
        }

        public void Initialize()
        {
            var grid = DiensteGrid;
            if (grid == null)
                return;

            var da = DataContext as YearOverviewViewModel;
            if (da == null)
                return;

            var Collection = da.Collection;
            if (Collection == null)
                return;

            var planItems = new List<IList<PlanItem>>();
            foreach (var pi in Collection)
                planItems.Add(new ObservableCollection<PlanItem>(pi));

            grid.Columns.Clear();

            if (planItems.Count <= 0)
                return;

            int index = 0;
            foreach (var column in planItems[0])
            {
                var col = new DataGridPlanItemTemplateColumn
                {
                    IsReadOnly = true,
                    CanUserReorder = false,

                    Header = column.Datum.Day,

                    ColumnName = string.Format("[{0}]", index),
                    CellTemplate = (DataTemplate)FindResource("PART_CellTemplate")
                };

                index++;
                grid.Columns.Add(col);
            }

            grid.ItemsSource = planItems;
        }

		public FrameworkElement Grids
		{
			get
			{
				return grids;
			}
		}
	}
}
